import { Config } from "../Config";
import { Player } from "../game/core/Player";
import Utils from "../game/GameUtils";
import { Watcher } from "../game/other/Watcher";
import MySQLService from "../service/MySQLService";
import Logger from "../utils/Logger";
import HallMgr from "./HallMgr";
import { Room, RoomState } from "./Room";

export class User {

    username: string;
    score: number = 100;
    room: Room;
    connection:any;     //websocket连接通道
    
    is_onLine:boolean = true;   //是否在线
    player:Player = null;       //对局中的玩家角色
    watcher:Watcher = null;     //观战者

    constructor(connection:any) {
        this.connection = connection;
        //收到消息
        connection.on('text', this.onMessage.bind(this));
        //关闭监听
        connection.on('close', this.onDropLine.bind(this));
        //掉线监听
        connection.on('error', this.onDropLine.bind(this));
    }

    /** 用户上线，接收用户名去绑定connection */
    userBind(obj:any){
        this.username =  obj.data.username;
        MySQLService.updateUserLoginState(this.username,1);
        HallMgr.users.set(this.username,this);
        HallMgr.updateOnline();
        this.initSpendData()
    }

    /** 行动消耗数据初始化，以后平衡性调整只需要修改服务端 */
    initSpendData() {
        let data = {
            COST_BORN_BIG: Config.COST_BORN_BIG,
            COST_BIG_MOVE: Config.COST_BIG_MOVE,
            COST_BIG_ATTACK: Config.COST_BIG_ATTACK,
            COST_BIG_EVOLVE_幽灵: Config.COST_BIG_EVOLVE_幽灵,
            COST_幽灵_MOVE: Config.COST_幽灵_MOVE,
            COST_幽灵_EVOLVE_恶灵: Config.COST_幽灵_EVOLVE_恶灵,
            COST_恶灵_ATTACK: Config.COST_恶灵_ATTACK,
            COST_恶灵_POSSESS: Config.COST_恶灵_POSSESS,
            COST_BIG_EVOLVE_炮台: Config.COST_BIG_EVOLVE_炮台,
            COST_炮台_IMPACT: Config.COST_炮台_IMPACT,
            COST_炮台_EVOLVE_狙炮: Config.COST_炮台_EVOLVE_狙炮,
            COST_狙炮_AIM: Config.COST_狙炮_AIM,
            COST_BIG_EVOLVE_顽石: Config.COST_BIG_EVOLVE_顽石,
            COST_BIG_EVOLVE_地灵: Config.COST_BIG_EVOLVE_地灵,
            COST_地灵_move: Config.COST_地灵_move,
            COST_地灵_抹平: Config.COST_地灵_抹平,
            COST_地灵_占据: Config.COST_地灵_占据,
            COST_BIG_EVOLVE_精灵: Config.COST_BIG_EVOLVE_精灵,
            COST_精灵_赐福: Config.COST_精灵_赐福,
            COST_精灵_庇护: Config.COST_精灵_庇护,
            COST_BIG_EVOLVE_沙兵: Config.COST_BIG_EVOLVE_沙兵,
            COST_沙兵_EVOLVE_沙皇: Config.COST_沙兵_EVOLVE_沙皇,
            COST_BIG_EVOLVE_野猪: Config.COST_BIG_EVOLVE_野猪,
            COST_野猪_IMPACT: Config.COST_野猪_IMPACT,
            COST_BIG_EVOLVE_野马: Config.COST_BIG_EVOLVE_野马,
            COST_野马_IMPACT: Config.COST_野马_IMPACT,
            COST_BIG_EVOLVE_拳师: Config.COST_BIG_EVOLVE_拳师,
            COST_拳师_移动: Config.COST_拳师_移动,
            COST_拳师_怒威神拳: Config.COST_拳师_怒威神拳,
            COST_拳师_爆裂火拳: Config.COST_拳师_爆裂火拳,
            COST_BIG_EVOLVE_灵阵: Config.COST_BIG_EVOLVE_灵阵,
            COST_灵阵_迷雾森林: Config.COST_灵阵_迷雾森林,
            COST_灵阵_九曲黄河: Config.COST_灵阵_九曲黄河,
            COST_灵阵_地狱杀伐: Config.COST_灵阵_地狱杀伐,
            COST_BIG_EVOLVE_天谕: Config.COST_BIG_EVOLVE_天谕,
            COST_天谕_明魂搜根: Config.COST_天谕_明魂搜根,
            COST_天谕_天眼传真: Config.COST_天谕_天眼传真,
            COST_天谕_神兵天降: Config.COST_天谕_神兵天降,
            COST_BIG_EVOLVE_妖术: Config.COST_BIG_EVOLVE_妖术,
            COST_妖术_绝命献祭: Config.COST_妖术_绝命献祭,
            COST_妖术_玄冰禁锢: Config.COST_妖术_玄冰禁锢,
            COST_妖术_瘟疫传播: Config.COST_妖术_瘟疫传播,
            COST_BIG_EVOLVE_鼓舞: Config.COST_BIG_EVOLVE_鼓舞,
            COST_鼓舞_嗜血狂战: Config.COST_鼓舞_嗜血狂战,
            COST_鼓舞_乘风破浪: Config.COST_鼓舞_乘风破浪,
            COST_鼓舞_生生不息: Config.COST_鼓舞_生生不息,
            COST_鼓舞_常胜将军: Config.COST_鼓舞_常胜将军
        }
        let obj = Utils.getObj("initSpendData",data,false)
        this.sendObj(obj)
    }

    /** 用户下线，修改登录状态 */
    onDropLine(code: number) {
        if(!this.is_onLine){
            return;
        }
        this.is_onLine = false;
        HallMgr.users.delete(this.username);
        HallMgr.updateOnline();
        if(this.room != null) {
            if(this.room.state == RoomState.MATCHING){
                this.room.cancelMatch(this);            //匹配中掉线
            }else if(this.room.state == RoomState.GAMEING){
                this.room.checkPlayersOnLineState(this);    //游戏中掉线
            }
        }
        MySQLService.updateUserLoginState(this.username,0);
        this.gc()
    }

    gc() {
        this.connection = null;
        this.room = null;
        this.player?.gc();
        this.watcher?.gc();
        this.player = null;
        this.watcher = null;
    }

    sendObj(obj:Object) {
        Logger.log("sendObj:",obj)
        this.connection.sendText(JSON.stringify(obj));
    }

    /**
     * 收到的message
     * isRoom   是否是房间内消息
     * type     data类型
     * data     数据
     */
    onMessage(result: string) {
        Logger.log("onMessage: "+result);
        let obj = JSON.parse(result);
        if(obj.isGame){
            this.room.gameMgr.reciveObj(obj,this);
        }else{
            switch(obj.method){
                case "userBind":            this.userBind(obj);   break;
                case "match":               HallMgr.match(this);    break;
                case "createOrjoinRoom":    HallMgr.createOrjoinRoom(this,obj.data.cipher);     break;
                case "watchingRoom":        HallMgr.watchingRoom(this);    break;
                case "cancelMatch":         this.room?.cancelMatch(this);        break;
                case "chat":                HallMgr.chat(obj);  break;
                case "admitDefeat":         this.room.admitDefeat(this);        break;
                default : console.error("webSocket 收到未知方法")
            }
        }
    }

}